<?php
require_once("loginView.php");
require_once(CORE_DIR.'userModel.php');

class loginAction extends Action{

	function __construct(){
		$this->view = new loginView();
	}

	public function run($get, $post, $viewtype) {
		parent::run($get,$post,$viewtype);

		$uModel = new userModel();
		
		if (isset($get["fbuid"]) && $get["fbuid"] != "") {
			$userDto = $uModel->fblogin($get["fbuid"]);
		}
		else {
			$userDto = $uModel->login($get["email"], $get["pass"]);
		}
		
		if($userDto)
		{
			$uModel->setActiveUser($userDto);
			$activeUser = $uModel->getActiveUser();
			$role = 0;
			
			if ($activeUser != null)
				$role = $activeUser->getRole();

			$hac = $uModel->hasAdminContent($activeUser->getId(), $role);
			
			if($get["remember"] == 1)
			{
				$this->logger->addLog("cmd: LOGIN REMEMBER session: ".session_id()." user: ".$activeUser->getId()."|".$activeUser->getNickName(), Logger::DEBUG_LEVEL);
				$uModel->saveAutologinData($activeUser->getId(), session_id());
			}
			else
			{
				$this->logger->addLog("cmd: LOGIN session: ".session_id()." user: ".$activeUser->getId()."|".$activeUser->getNickName(), Logger::DEBUG_LEVEL);
			}
			
			$this->view->setProperty("userid", $activeUser->getId());
			$this->view->setProperty("userName", $activeUser->getNickname());
			$this->view->setProperty("role", $role);
			$this->view->setProperty("hasAdmContent", $hac);
			$this->view->showLogin();
		}
		else 
		{
			$this->view->showNotLoggedin();
		}
			
	}
}
?>